const regHznzi =
  /[\u4e00-\u9fa5|\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]/;

export function decodeColumns(columns) {
  return columns.map((item) => ({
    name: item.title,
    childList: item.children
      .map((li) => ({
        name: li.title,
        code: li.dataIndex,
        selected: li.selected
      }))
      .filter((item) => item.dataIndex !== "columnsAction")
  }));
}

export function selectAllColumns(idx, columns, checked) {
  const columnsTemp = JSON.parse(JSON.stringify(columns));
  columnsTemp[idx].selectAll = checked;
  columnsTemp[idx].children = columnsTemp[idx].children.map((item) => ({
    ...item,
    selected: ["name", "idCard"].includes(item.dataIndex) ? true : checked
  }));
  return columnsTemp;
}

export function updateColumns(columns) {
  const columnsTemp = JSON.parse(JSON.stringify(columns));
  return {
    columnsFull: columnsTemp,
    columnsSelected: generateColumns(columnsTemp)
  };
}

export function selectedToggleColumns(subIdx, idx, columns) {
  const columnsTemp = JSON.parse(JSON.stringify(columns));
  columnsTemp[idx].children[subIdx] = {
    ...columnsTemp[idx].children[subIdx],
    selected: !columnsTemp[idx].children[subIdx].selected
  };
  columnsTemp[idx].selectAll = columnsTemp[idx].children.length === columnsTemp[idx].children.filter((item) => item.selected).length;
  return columnsTemp;
}

export function buildColumns(list) {
  const res = [];
  for (let i = 0; i < list.length; i++) {
    const item = list[i];
    const children = [];
    let selectAll = true;
    for (let j = 0; j < item.childList.length; j++) {
      const subItem = item.childList[j];
      const subItemNew = {
        title: subItem.name,
        dataIndex: subItem.code,
        selected: subItem.selected,
        disabled: ["name", "idCard"].includes(subItem.code),
        ellipsis: true,
        width: 200
      };
      if (regHznzi.test(subItem.code)) {
        subItemNew["customRender"] = (text, record) => {
          return record.data[subItem.code];
        };
      }
      children.push(subItemNew);
      if (!subItem.selected) {
        selectAll = false;
      }
    }
    res.push({
      title: item.name,
      dataIndex: `group_${i}`,
      children: children,
      selectAll: selectAll
    });
  }
  return {
    columnsFull: res,
    columnsSelected: generateColumns(res, true)
  };
}

function generateColumns(columns, isInit = false) {
  const columnsNew = columns.map((item) => {
    const children = item.children.filter((li) => li.selected);
    if (children.length > 0) {
      return {
        ...item,
        children
      };
    } else {
      return false;
    }
  });
  const columnsNew2 = columnsNew.filter((item) => item);
  if (isInit) {
    columnsNew2.push({
      title: "操作",
      dataIndex: "columnsAction",
      fixed: "right",
      scopedSlots: { customRender: "columnsAction" }
    });
  }
  return columnsNew2;
}
